<?php
namespace app\index\controller;

class Product extends Common
{
    public function groupbuy()
    {
        $menber_id = session('id');
        $count = db('shopcar')->where('menber_id',$menber_id)->count();
        $shop = db('shopcar')->where('menber_id',$menber_id)->select();
        $this->assign('shop',$shop);
        $this->assign('count',$count);
        return view();
    }
    public function orders()
    {
        $user_id = session('id');
        $address = db('menber')->alias('a')->join('address b','a.id = b.user_id')->where('user_id',$user_id)->select();
        $shop = db('shopcar')->where('menber_id',$user_id)->select();
        $count = db('shopcar')->where('menber_id',$user_id)->count();
        $this->assign('address',$address);
        $this->assign('count',$count);
        $this->assign('shop',$shop);
        return view();
    }
    public function productdetailed()
    {

        $shop_id = input('get.id');
        $menber_id = session('id');
        $count = db('shopcar')->where('menber_id',$menber_id)->count();
        $shopcar = db('shopcar')->where('menber_id',$menber_id)->select();
        $message = db('shop_message')->alias('a')->join('menber b','a.user_id=b.id')->where('product_id',$shop_id)->select();
        $specifications=db('shoper_specifications')->where('product_id',$shop_id)->select();
        $specifications1=db('shoper_specifications_attribute')->where('product_id',$shop_id)->select();
        foreach ($specifications as $key1 => $value1) {
            foreach ($specifications1 as $key2 => $value2) {
                if($value1['guige_id']==$value2['guige_id']){
                    $specifications[$key1]['son'][]=$value2;
                }         
            }
        } 
        // dump($specifications[1]);die;
        $this->assign('specifications',$specifications);
        $this->assign('shop',$shopcar);
        $this->assign('message',$message);
        $this->assign('count',$count);
        $shoper = db('shoper')->where('id',$shop_id)->select();
        $this->assign('shoper',$shoper);

        $mess_count = db('shop_message')->where('product_id',$shop_id)->count();
        $this->assign('mess_count',$mess_count);
        return view();
    }
    public function productlist()
    {
        $menber_id = session('id');
        $count = db('shopcar')->where('menber_id',$menber_id)->count();
        $shop = db('shopcar')->where('menber_id',$menber_id)->select();
        $shoper = db('cat')->alias('a')->join('shoper b','a.id=b.cat_id')->select();
        $this->assign('shoper',$shoper);
        $this->assign('shop',$shop);
        $this->assign('count',$count);
        return view();
    }
    public function shoppingcart()
    {
        $menber_id = session('id');
        $count = db('shopcar')->where('menber_id',$menber_id)->count();
        $shop = db('shopcar')->where('menber_id',$menber_id)->select();
        $this->assign('shop',$shop);
        $this->assign('count',$count);
        return view();
    }

    public function insertcart()
    {
        $num = input('get.num');
        $discount = input('get.discount');
        $img = input('get.midimg');
        $shopname = input('get.shopname');
        $price = input('get.price');
        $menber_id = session('id');
       
        $shoper = db('shoper')->where('shopname',$shopname)->select();
        $shop_id = '';
        foreach ($shoper as $key => $value) {
            $shop_id = $value['id'];
        }
        $data =[
            'id'=>$shop_id,
            'discount'=>$discount,
            'price'=>$price,
            'img'=>$img,
            'name'=>$shopname,
            'number'=>$num,
            'menber_id'=>$menber_id

        ];
        $shop = db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->select();
        if (!$shop) {
            db('shopcar')->insert($data);
            echo '添加成功！';
        }else {
            db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->update(['number'=>$num]);
            echo '添加成功！';
        }

    }
    public function ordernew()
    {
        $num = input('get.num');
        $discount = input('get.discount');
        $img = input('get.midimg');
        $shopname = input('get.shopname');
        $price = input('get.price');
        $menber_id = session('id');

        $shoper = db('shoper')->where('shopname',$shopname)->select();
        $shop_id = '';
        
        foreach ($shoper as $key => $value) {
            $shop_id = $value['id'];
        }
        $data =[
            'id'=>$shop_id,
            'discount'=>$discount,
            'price'=>$price,
            'img'=>$img,
            'name'=>$shopname,
            'number'=>$num,
            'menber_id'=>$menber_id

        ];
       
        $shop = db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->select();
        if ($shop) {
            db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->update(['number'=>$num]);
            // $this->success('添加成功！',url('shoppingcart'));
           
        }else {
            db('shopcar')->insert($data);
            // $this->success('添加成功！',url('shoppingcart'));
        }

    }
    public function updatenum()
    {
        $menber_id = session('id');
        $shop_id = input('get.shop_id');
        $number = input('get.num');
        db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->update(['number'=>$number]);
    }
    public function delshop()
    {
        $menber_id = session('id');
        $shop_id = input('get.shop_id');
        $del = db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->delete();
        if($del){
            echo '删除成功';
        }
    }
    public function Collect()
    {
        
        $menber_id = session('id');
        $shop_id = input('get.shop_id');
        $shop = db('shoper')->where('id',$shop_id)->select();
        foreach ($shop as $key => $value) {
            $data = [
                'id'=>$shop_id,
                'shopname'=>$value['shopname'],
                'discount'=>$value['discount'],
                'price'=>$value['price'],
                'img'=>$value['src'],
                'menber_id'=>$menber_id,
            ];
        }

        $Collect = db('collect')->where('id',$shop_id)->where('menber_id',$menber_id)->select();
        if (!$Collect) {
            db('collect')->insert($data);
            echo '收藏成功！';
        }else {
            echo '您已收藏该商品，试试收藏其他商品！！';
        }
    }
    public function cart()
    {
        $menber_id = session('id');
        $shop_id = input('get.shop_id');
        $shop = db('shoper')->where('id',$shop_id)->select();
        foreach ($shop as $key => $value) {
            $data = [
                'id'=>$shop_id,
                'name'=>$value['shopname'],
                'discount'=>$value['discount'],
                'price'=>$value['price'],
                'img'=>$value['src'],
                'menber_id'=>$menber_id,
            ];
        }
        $shopcar = db('shopcar')->where('id',$shop_id)->where('menber_id',$menber_id)->select();
        $data['number']='1';
        if (!$shopcar) {
            db('shopcar')->insert($data);
            echo '加入购物车成功！';
        }else {
            echo '您已将该商品加入购物车！！';
        }

    }
    public function message()
    {
        $shop_id = input('get.shop_id');
        $message = input('get.message','','htmlspecialchars');
        $user_id = session('id');
        $inputtime = date('Y-m-d H:i:s',time());
        $data = [
            'message'=>$message,
            'time'=>$inputtime,
            'product_id'=>$shop_id,
            'user_id'=>$user_id,
        ];
        $res = db('shop_message')->insert($data);
        if ($res) {
            echo '评论成功！';
        }
    }


    public function page()
    {
        
        $menber_id = session('id');
        $total=input('get.total','','htmlspecialchars');
        $integral = input('get.integral','','htmlspecialchars');
        $message = input('get.mess','','htmlspecialchars');
        $inputtime = date('Y-m-d H:i:s',time());
        if ($total!=0) {
            $orders = date('Ymd',time()).rand(100000000,999999999);
            session('orders',$orders);
            session('integral',$integral);
            session('total',$total);
            session('message',$message);
            $shop = db('shopcar')->where('menber_id',$menber_id)->select();
            $data1 = [];
            foreach ($shop as $key => $value) {
                $data1 = [
                    'orders'=>session('orders'),
                    'shopname'=>$value['name'],
                    'discount'=>$value['discount'],
                    'price'=>$value['price'],
                    'number'=>$value['number'],
                    'img'=>$value['img'],
                    'user_id'=>$menber_id
                ];
                db('ordersinfo')->insert($data1);
            }
            $data2 = [
                'orders'=>session('orders'),
                'menber_id'=>$menber_id,
                'inputtime'=>$inputtime,
                'integral'=>session('integral'),
                'message'=>session('message'),
                'total'=>session('total'),

            ];
            $data2['state']="待发货";
            db('orders')->insert($data2);
            $user = db('menber')->where('id',$menber_id)->select();
            $orderintegral = 0;
            foreach ($user as $key => $value) {
                $orderintegral = $value['integral'];
            }
            $integrales = ($integral+$orderintegral);
            db('menber')->where('id',$menber_id)->update(['integral'=>$integrales]);
            
        }
        $this->delcart();
        $this->assign('total',session('total'));
        $this->assign('orders',session('orders'));
        return view();
    }
    public function delcart()
    {
        $menber_id = session('id');
        db('shopcar')->where('menber_id',$menber_id)->delete();
    }
    public function pagepay()
    {
        require_once EXTEND_PATH.'alipay/config.php';
        require_once EXTEND_PATH.'alipay/pagepay/service/AlipayTradeService.php';
        require_once EXTEND_PATH.'alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';

        //商户订单号，商户网站订单系统中唯一订单号，必填
        $out_trade_no = trim($_POST['WIDout_trade_no']);

        //订单名称，必填
        $subject = trim($_POST['WIDsubject']);

        //付款金额，必填
        $total_amount = trim($_POST['WIDtotal_amount']);

        //商品描述，可空
        $body = trim($_POST['WIDbody']);

        //构造参数
        $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
        $payRequestBuilder->setBody($body);
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setOutTradeNo($out_trade_no);

        $aop = new \AlipayTradeService($config);

        /**
         * pagePay 电脑网站支付请求
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @param $return_url 同步跳转地址，公网可以访问
         * @param $notify_url 异步通知地址，公网可以访问
         * @return $response 支付宝返回的信息
         */
        $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

        //输出表单
        var_dump($response);
    }
    public function guige(){
        $fir_id=input('get.fir_id');
        session('fir_id' , $fir_id);
        $sec_id=input('get.sec_id');
        $shoper_fixed_attr= db('shoper_fixed_attr')->where('sec_guigeid',$sec_id)->find();
        $price=$shoper_fixed_attr['price'];
        echo $sec_id;
    }

}